package com.xuzm.demo.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class HolidayUtils {
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 获取激活日期
     * @param today
     * @param list
     * @param num num个工作日后
     * @return
     */
    public static Date getScheduleActiveDate(Date today, List<String> list, int num)throws ParseException {
        Date toworrow = null;
        int delay = 1;
        while(delay<=num){
            toworrow = getTomorrow(today);
            if(!isWeekend(simpleDateFormat.format(toworrow))&&!isHoliday(simpleDateFormat.format(toworrow),list)){
                delay++;
                today = toworrow;
            }else if(isWeekend(simpleDateFormat.format(toworrow))){
                today= toworrow;
            }else if(isHoliday(simpleDateFormat.format(today),list)){
                today = toworrow;
            }
        }
        return  today;
    }

    /**
     * 获取tomorrow的日期
     * @param date
     * @return
     */
    public static Date getTomorrow(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH,+1);
        date = calendar.getTime();
        return date;
    }

    /**
     * 判断是否是weekend
     * @return
     * @throws ParseException
     */
    public static  boolean isWeekend(String sdate)throws ParseException{
        Date date = simpleDateFormat.parse(sdate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 判断是否是holiday
     * @param sdate
     * @param list
     * @return
     * @throws ParseException
     */
    public static boolean isHoliday(String sdate,List<String>list) throws ParseException{
        if(list.size()>0){
            for(int i = 0;i<list.size();i++){
                if(sdate.equals(list.get(i))){
                    return true;
                }
            }
        }
        return false;
    }
}
